from flask import Blueprint, render_template, request, current_app
from app.models.question import Question
from app.models.tag import Tag
from app.forms.question_forms import SearchForm
from app.utils import DataUtil, LogUtil

main = Blueprint('main', __name__)

@main.route('/')
def index():
    # 获取分页参数
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config.get('QUESTIONS_PER_PAGE', 10)
    
    # 获取问题列表
    query = Question.query.order_by(Question.create_time.desc())
    
    # 使用DataUtil工具类进行分页处理
    questions = DataUtil.paginate(query, page, per_page)
    
    # 获取热门问题
    hot_questions = Question.query.order_by(Question.view_count.desc()).limit(5).all()
    
    # 获取所有标签
    tags = Tag.query.all()
    
    search_form = SearchForm()
    
    LogUtil.info(f"首页访问: 展示第{page}页问题")
    
    return render_template('index.html', 
                        questions=questions,
                        hot_questions=hot_questions,
                        tags=tags,
                        search_form=search_form)

@main.route('/tag/<int:tag_id>')
def tag(tag_id):
    tag = Tag.query.get_or_404(tag_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config.get('QUESTIONS_PER_PAGE', 10)
    
    # 使用DataUtil工具类按标签过滤
    query = DataUtil.filter_by_tag(Question.query, tag_id).order_by(Question.create_time.desc())
    
    # 使用DataUtil工具类进行分页
    questions = DataUtil.paginate(query, page, per_page)
    
    hot_questions = Question.query.order_by(Question.view_count.desc()).limit(5).all()
    tags = Tag.query.all()
    
    search_form = SearchForm()
    
    LogUtil.info(f"标签页访问: 标签 {tag.name}(ID:{tag_id}), 第{page}页")
    
    return render_template('tag.html',
                        tag=tag,
                        questions=questions,
                        hot_questions=hot_questions,
                        tags=tags,
                        search_form=search_form)

@main.route('/search')
def search():
    search_term = request.args.get('query', '')
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config.get('QUESTIONS_PER_PAGE', 10)
    
    # 使用DataUtil工具类进行搜索
    if search_term:
        query = DataUtil.search(
            Question.query,
            search_term,
            [Question.title, Question.content]
        ).order_by(Question.create_time.desc())
    else:
        query = Question.query.order_by(Question.create_time.desc())
    
    # 使用DataUtil工具类进行分页
    questions = DataUtil.paginate(query, page, per_page)
    
    hot_questions = Question.query.order_by(Question.view_count.desc()).limit(5).all()
    tags = Tag.query.all()
    
    search_form = SearchForm()
    search_form.query.data = search_term
    
    LogUtil.info(f"搜索页访问: 关键词 '{search_term}', 第{page}页")
    
    return render_template('search.html',
                          search_term=search_term,
                          questions=questions,
                          hot_questions=hot_questions,
                          tags=tags,
                          search_form=search_form) 